<template>
  <div>
    <ComDialog
      width="600"
      @EventClose="closeFn"
      :dialogVisible="dialogVisible"
      :title="isEdit ? '修改问金发放' : '新增问金发放'"
    >
      <el-form label-width="100px" :model="addForm">
        <el-form-item label="商务姓名" prop="region">
          <el-select v-model="addForm.postion" placeholder="请选择活动区域">
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配比比例" prop="ratio">
          <el-input v-model="addForm.ratio"></el-input>
        </el-form-item>
        <el-form-item label="职等id" prop="level">
          <el-input v-model="addForm.level"></el-input>
        </el-form-item>
      </el-form>
      <template name="footer">
        <el-button @click="closeFn"> 取消 </el-button>
        <el-button type="primary" @click="saveAdditionFn"> 保存 </el-button>
      </template>
    </ComDialog>
  </div>
</template>

<script>
import ComDialog from "../../../components/comDialog.vue";
import {
  getVoucherRatioGetInfoApi,
  getVoucherRatioEditApi,
  getVoucherRatioAddApi,
} from "@/wss/api/financial/voucherRatio.js";
import {
  getPositionListApi,
  getPositionGetInfoApi,
} from "@/wss/api/HumanResources/roleSetting/index.js";

export default {
  props: {
    selectOption: {
      type: Array,
      default: () => [],
    },
    dialogVisible: {
      default: false,
      type: Boolean,
    },
  },
  components: {
    ComDialog,
    // 新增问金发放数据
  },
  data() {
    return {
      // 判断是否是修改
      isEdit: false,
      addForm: {
        postion: "",
        level: "",
        ratio: "",
      },
      options: [],
    };
  },

  created() {
    this.getPositionListFn();
  },
  methods: {
    // 获取角色列表
    async getPositionListFn() {
      let { rows, code } = await getPositionListApi();
      if (code === 200) {
        rows.forEach((item) => {
          this.options.push({ label: item.name, value: item.idStr });
        });
        console.log(rows, "函数");
      }
    },
    // 根据id获取详细
    async getDetailFn(row) {
      console.log(row);

      let { data } = await getVoucherRatioGetInfoApi({ id: row.idStr });
      // console.log(result);
      this.isEdit = true;
      this.addForm = data;
      console.log(this.addForm, "hhh");
      this.options.forEach((item) => {
        if (data.postionName === item.label) {
          this.addForm.postion = item.value;
        }
      });
    },
    // 关闭对话框时初始化
    closeFn() {
      this.isEdit = false;
      (this.addForm = {
        postion: "",
        level: "",
        ratio: "",
      }),
        this.$emit("update:dialogVisible", false);
    },
    // 刷新父组件的list
    fretchList(msg) {
      this.closeFn();
      this.$modal.msgSuccess(msg);
      this.$emit("EventFretchList");
    },
    // 新增或者修改
    async saveAdditionFn() {
      console.log(this.addForm, "添加");
      // 如果有就是修改
      if (this.addForm.id) {
        // []
        this.addForm.id = this.addForm.idStr;
        let { code, msg } = await getVoucherRatioEditApi(this.addForm);

        // result
        if (code === 200) {
          this.fretchList(msg);
        }
      } else {
        let { msg, code } = await getVoucherRatioAddApi(this.addForm);
        if (code === 200) {
          this.fretchList(msg);
        }
      }
    },
  },
};
</script>

<style>
</style>